package com.wangjiyuan.shiwupai.ui_food.food.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.bean.first_food.FoodBean;
import com.wangjiyuan.shiwupai.utils.RecyclerViewAdapterHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by QXL on 2016/11/28.
 */


public class FoodRecyclerAdapter extends RecyclerViewAdapterHelper<Object> {
//    0 头布局
    public static final int TYPE0 = 0, TYPE1 = 1;
    public static final String TITLE[] = {"","食物分类", "热门品牌", "连锁餐饮"};
    private ItemFoodRecyclerAdapter itemFoodRecyclerAdapter;

    public FoodRecyclerAdapter(Context context, List<Object> list) {
        super(context, list);

    }

//    多布局
    @Override
    public int getItemViewType(int position) {
        if (mList.get(position) instanceof Boolean) {
            return TYPE0;
        } else {
            return TYPE1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE0:
                view = mInflater.inflate(R.layout.itemhead_food, parent, false);
                return new ViewHolderHead(view);
            case TYPE1:
                view = mInflater.inflate(R.layout.item_food, parent, false);
                return new ViewHolderFood(view);
        }
        return null;
    }

    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolderFood){
            ((ViewHolderFood) holder).textView_item_food.setText(TITLE[position]);
            FoodBean.GroupBean groupBean = (FoodBean.GroupBean) mList.get(position);
            String kind = groupBean.getKind();
            List<FoodBean.GroupBean.CategoriesBean> categories = groupBean.getCategories();
//            通过构造将kind传入
            itemFoodRecyclerAdapter=new ItemFoodRecyclerAdapter(mContext,categories,kind);
            ((ViewHolderFood) holder).recyclerView_item_food.setAdapter(itemFoodRecyclerAdapter);
        }

    }

    class ViewHolderHead extends RecyclerView.ViewHolder {
        private TextView textView_searchFood, searchFood;
        private Button food_analysis, search_contrast, code_contrast;

        public ViewHolderHead(View itemView) {
            super(itemView);
            textView_searchFood = (TextView) itemView.findViewById(R.id.textView_searchFood);
            searchFood = (TextView) itemView.findViewById(R.id.searchFood);
            textView_searchFood.setPadding(0, 150, 10, 0);
            searchFood.setPadding(0, 25, 0, 25);
        }
    }

    class ViewHolderFood extends RecyclerView.ViewHolder {
        TextView textView_item_food;
        RecyclerView recyclerView_item_food;

        public ViewHolderFood(View itemView) {
            super(itemView);
            textView_item_food = (TextView) itemView.findViewById(R.id.textView_item_food);
//            RecyclerView嵌套一个GridLayoutManager的RecyclerView
            recyclerView_item_food = (RecyclerView) itemView.findViewById(R.id.recyclerView_item_food);
            recyclerView_item_food.setHasFixedSize(true);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3, OrientationHelper.VERTICAL, false);
            recyclerView_item_food.setLayoutManager(gridLayoutManager);
        }
    }
}
